<template>
  <div
    ref="imageItem"
    class="image-item"
    :data-index="index"
    @mouseenter.stop="over"
    @mouseleave.stop="out"
    @click.prevent="clickImage"
  >
    <p class="image" :style="{'background-image':'url('+data.url+')'}" />
    <p class="name">{{ data.filename }}</p>
    <div class="top-handle">
      <p ref="selectBox" :class="select_box_class" />
      <p ref="delete" class="delete" :class="overing ? 'show' : ''" @click.stop="remove"><svg-icon style="width: 20px;height: 20px;" icon-class="remove" /></p>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ImageItem',
  props: {
    data: {
      type: Object
    },
    index: {
      type: Number
    },
    limit: {
      type: Number
    },
    // 已经选择的图片数量
    number: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      overing: 0
    }
  },
  computed: {
    select_box_class() {
      let box_class = 'select-box'
      if (this.overing) {
        box_class += ' show'
      }
      if (this.data.selected) {
        box_class += ' show selected'
      }
      return box_class
    }
  },
  methods: {
    // 鼠标移入图片
    over() {
      this.overing = 1
    },
    // 鼠标移出图片
    out() {
      this.overing = 0
    },
    // 点击选中图片
    clickImage() {
      if (this.data.selected === 1) {
        this.$emit('unselect', this.$refs.imageItem.getAttribute('data-index'))
      } else {
        if (this.number >= this.limit) {
          this.$emit('overstep')
        }
        this.$emit('select', this.$refs.imageItem.getAttribute('data-index'))
      }
    },
    // 删除附件
    remove() {
      this.$confirm('确认要删除此附件吗？', '提示')
        .then(() => {
          this.$del('attachment/delete/' + this.data.id)
            .then(() => {
              this.$emit('remove', this.$refs.imageItem.getAttribute('data-index'))
              this.$message.success('删除成功')
            })
            .catch((res) => {
              this.$handleError(res)
            })
        })
    }
  }
}
</script>

<style scoped lang="scss">
.image-item{
  position: relative;
  width: 140px;
  height: 140px;
  float: left;
  margin: 0 10px 10px 0;
  cursor: pointer;
  &:nth-of-type(6n) {
    margin-right: 0;
  }
  p{
    margin: 0;
    cursor: pointer;
    &.image{
      width: 100%;
      height: 100%;
      background: #eee no-repeat center center;
      background-size: contain;
      border: 5px solid #eee;
    }
    &.name{
      position: absolute;
      bottom: 0;
      width: 100%;
      height: 20px;
      line-height: 20px;
      font-size: 12px;
      padding: 0 5px;
      box-sizing: border-box;
      background: rgba(0,0,0,.5);
      color: #fff;
      text-overflow: ellipsis;
      overflow: hidden;
      white-space: nowrap;
    }
  }
  .top-handle{
    position: absolute;
    top:0;
    width: 100%;
    height: 30px;
    .select-box{
      position: absolute;
      top:5px;
      left:5px;
      width: 20px;
      height: 20px;
      border-radius: 3px;
      background: #fff;
      display: none;
      &.show{
        display: block;
      }
      &.selected::before{
          display: block;
          content: "";
          width: 100%;
          height: 100%;
          background: #409EFF url('../../svg/selected.svg') no-repeat center center;
          background-size: 70%;
          border-radius: 3px;
      }
    }
    .delete{
      position: absolute;
      right: 5px;
      top:5px;
      display: none;
      &.show{
        display: block;
      }
    }
  }
}
</style>
